import tkinter as tk


def calculate_sum():
    # 获取输入框中的值，并转换为浮点数
    try:
        num1 = float(entry1.get())
        num2 = float(entry2.get())
        num3 = float(entry3.get())
    except ValueError:
        # 如果转换失败，显示错误消息
        result_label.config(text="请输入有效的数字")
        return

        # 计算总和
    total = num1 + num2 + num3

    # 显示总和
    result_label.config(text=f"总和: {total:.2f}")


# 创建主窗口
root = tk.Tk()
root.title("数值输入界面")

# 创建并放置第一个输入框
label1 = tk.Label(root, text="数值1:")
label1.grid(row=0, column=0, padx=10, pady=10)
entry1 = tk.Entry(root)
entry1.grid(row=0, column=1, padx=10, pady=10)

# 创建并放置第二个输入框
label2 = tk.Label(root, text="数值2:")
label2.grid(row=1, column=0, padx=10, pady=10)
entry2 = tk.Entry(root)
entry2.grid(row=1, column=1, padx=10, pady=10)

# 创建并放置第三个输入框
label3 = tk.Label(root, text="数值3:")
label3.grid(row=2, column=0, padx=10, pady=10)
entry3 = tk.Entry(root)
entry3.grid(row=2, column=1, padx=10, pady=10)

# 创建并放置计算按钮
calculate_button = tk.Button(root, text="计算总和", command=calculate_sum)
calculate_button.grid(row=3, column=0, columnspan=2, pady=20)

# 创建并放置结果标签
result_label = tk.Label(root, text="")
result_label.grid(row=4, column=0, columnspan=2, pady=10)

# 运行主循环
root.mainloop()